<?php

namespace App\Http\Controllers\Admin;

use App\Models\Currency;
use App\Models\CurrencyModify;
use App\Models\Turn;
use App\Models\User;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Property;
use Cloud;
use OSS\OssClient;


/**
 * 货币管理
 */
class CurrencyController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'currency');
    }

    /**
     * 货币列表
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/12
     * @access public
     * @return array        所有货币信息
     */
    public function index(Request $request)
    {
        //		$type = $request->get('type') ?? 1;
        //		$data = [];
        $data = Currency::paginate(10);
        $list = [];
        foreach ($data as $k=>$v){
            $list[$k]['id'] = $v->id;
            $list[$k]['logo'] = config('alioss.host') . '/' . $v->logo;
            if ($v->online_switch==1){
                $list[$k]['online_switch'] = '开启';
            }else{
                $list[$k]['online_switch'] = '关闭';
            }
            if ($v->pay_switch==1){
                $list[$k]['pay_switch'] = '开启';
            }else{
                $list[$k]['pay_switch'] = '关闭';
            }

            $list[$k]['name'] = $v->name;
            $list[$k]['name_en'] = $v->name_en;
            $list[$k]['total'] = $v->total;
            $list[$k]['fee'] = $v->fee;
            $n_p = Order::where('cid',$v->id)->orderBy('id','desc')->first();
            if($n_p){
                $list[$k]['new_p'] = (float)$n_p->price;
            }else{
                $list[$k]['new_p'] = '-';
            }
            $list[$k]['end_p'] = $list[$k]['new_p'];
            $list[$k]['start_p'] = $list[$k]['new_p'];
            $list[$k]['updated_at'] = $v->updated_at;
        }
//        die;

        return view('admin/currency/index', compact('list','data'));
    }


    /**
     * 货币修改记录
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/5/16
     * @access public
     * @return array        所有货币信息
     */
    public function modify(Request $request)
    {
        $id = $request->get('id');
        if (!empty($id)){
            $data = CurrencyModify::where('cid',$id)->paginate(10);
        }else{
            $data = CurrencyModify::paginate(10);
        }
        $curr = Currency::get(['id','name_en']);

        $list = [];
        foreach ($data as $k=>$v){
            $list[$k]['id'] = $v->id;
            $list[$k]['logo'] = config('alioss.host') . '/' . $v->logo;
            if ($v->online_switch==1){
                $list[$k]['online_switch'] = '开启';
            }else{
                $list[$k]['online_switch'] = '关闭';
            }
            if ($v->pay_switch==1){
                $list[$k]['pay_switch'] = '开启';
            }else{
                $list[$k]['pay_switch'] = '关闭';
            }

            $list[$k]['name'] = $v->name;
            $list[$k]['name_en'] = $v->name_en;
            $list[$k]['total'] = $v->total;
            $list[$k]['fee'] = $v->fee;
            $list[$k]['created_at'] = $v->created_at;
            $list[$k]['official_price'] = $v->official_price;
            $list[$k]['total'] = $v->total;
            $list[$k]['max_quota'] = $v->max_quota;
        }
//        die;

        return view('admin/currency/modify', compact('list','data','curr'));
    }
    /**
     * 添加修改货币页面
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/12
     * @access public
     */
    public function currency_add_view(Request $request)
    {
        return view('admin/currency/currency_add_view');
    }

    /**
     * 添加修改货币页面
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/12
     * @access public
     * @param  int    id    货币ID
     * @return array        货币详情
     */
    public function currency_editor(Request $request)
    {
        //$info = $request->all();
        //dump($info);die;
        $id = $request->get('id') ?? 0;
        $map1['id'] = $id;
        $infos       = Currency::where($map1)->first();
        //dd($infos);die;
        $infos->logo = config('alioss.host') . '/' . $infos->logo;
        return view('admin/currency/currency_editor', compact('infos'));
    }

    /**
     * 添加货币
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/12
     * @access public
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return array        执行结果
     */
    public function currency_add(Request $request)
    {
        $info = $request->all();
        if (empty($info['name']) || empty($info['name_en']) || empty($info['orderby']) || empty($info['max_quota']) || empty($info['total']) || empty($info['fee']) || empty($info['unit']) || empty($info['official_price'])
            || empty($info['into_value'])|| empty($info['out_value'])) {
            return back()->with(['msg'=>'请填写带*标记内容','status'=>0]);
        }
        $oss = new OssClient(config('alioss.accessKeyId'), config('alioss.accessKeySecret'), config('alioss.endpoint'));
        $date = [
            'name' => $info['name'],
            'name_en' => strtoupper($info['name_en']),
            'orderby' => $info['orderby'],
            'total' => $info['total'],
            'fee' => $info['fee'],
            'into_fee' => $info['into_fee'],
            'out_fee' => $info['out_fee'],
            'unit' => $info['unit'],
            'max_quota' => $info['max_quota'],
            'official_price'=>$info['official_price'],
            'into_value'=>$info['into_value'],
            'out_value'=>$info['out_value'],
        ];
        if ($request->has('logo')) {
            $logo = $request->file('logo');
            if ($logo) {
                if ($logo->isValid()) {
                    $realPath = $logo->getRealPath(); //临时文件的绝对路径
                    $ext      = $logo->getClientOriginalExtension(); // 扩展名
                    $filename = date('YmdHis') . '_' . uniqid() . '.' . $ext; //文件名
                    $osspath  = 'block_chain/' . $filename; //oss路径
                    $oss->uploadFile(config('alioss.bucket'), $osspath, $realPath); //上传文件到阿里云
                    $date['logo'] = $osspath; //数据库保存非全路径文件名
                }
            }
        }


        //开关类参数

        if (!empty($info['online_switch'])) {
            $date['online_switch'] = 1;
        } else {
            $date['online_switch'] = 0;
        }
        if (!empty($info['pay_switch'])) {
            $date['pay_switch'] = 1;
        } else {
            $date['pay_switch'] = 0;
        }
        if (!empty($info['buy_switch'])) {
            $date['buy_switch'] = 1;
        } else {
            $date['buy_switch'] = 0;
        }
        if (!empty($info['is_standard'])) {
            $date['is_standard'] = $info['is_standard'];
        } else {
            $date['is_standard'] = 0;
        }
        $count = Currency::where('name_en',$date['name_en'])->count();
        if (!empty($info['id'])) {//编辑
            if ($count>1)
                return back()->with(['msg'=>'已添加该货币','status'=>0]);
            $id = $info['id'];
            $result = Currency::where('id', $id)->update($date);
            $curr = Currency::find($id);
            $date['logo'] = $curr->logo;
            $date['cid'] = $curr->id;
        } else {//新增
            if ($count>0)
                return back()->with(['msg'=>'已添加该货币','status'=>0]);
            $result = Currency::create($date);
            $date['cid'] = $result->id;
        }
        CurrencyModify::create($date);
        if ($result) {
//            return ['msg'=>'添加成功','status'=>1];
            return back()->with(['msg'=>'添加成功','status'=>1,'msg_url'=>'currency_list']);

        } else {
//            return ['msg'=>'添加失败','status'=>0];
            return back()->with(['msg'=>'添加失败','status'=>1,'msg_url'=>'currency_add']);

        }
//        return view('admin/user/add_user');
    }

    /**
 * 充值记录
 *
 * @ author: zrc
 * @ E-mail:904725327@qq.com
 * @ date: 2018/3/12
 * @access public|private|protected
 * @param  mixed    name    comment
 * @param  int    name    comment
 * @param  string    name    comment
 * @param  bool       name    comment
 * @param  array   name    comment
 * @return void|int|string|boolean|array        comment
 */
    public function currency_recharge_info(Request $request)
    {
        $account = $request->get('account');
        $cid = $request->get('cid');

        $data = Turn::where(['type' => 1,'status' => 2])
        ->where(function ($query) use ($account) {
            if (!empty($account)) {
                $uids = User::where('account', 'like', '%'.$account.'%')->pluck('id')->toArray();
                $query->whereIn('uid', $uids);
            }
        })
        ->where(function ($query) use ($cid) {
            if (!empty($cid) && $cid != 0) {
                $query->where('cid', $cid);
            }
        })
        ->paginate(10);

        $currs = Currency::select('id', 'name_en')->get();

        return view('admin/currency/currency_recharge_info', compact('data', 'currs', 'account', 'cid'));
    }

    /**
     * 充值记录
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/12
     * @access public|private|protected
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function currency_extract_info(Request $request)
    {
        $account = $request->get('account');
        $cid = $request->get('cid');

        $data = Turn::where(['type' => 2,'status' => 2])
        ->where(function ($query) use ($account) {
            if (!empty($account)) {
                $uids = User::where('account', 'like', '%'.$account.'%')->pluck('id')->toArray();
                $query->whereIn('uid', $uids);
            }
        })
        ->where(function ($query) use ($cid) {
            if (!empty($cid) && $cid != 0) {
                $query->where('cid', $cid);
            }
        })
        ->paginate(10);

        $currs = Currency::select('id', 'name_en')->get();

        return view('admin/currency/currency_extract_info', compact('data', 'currs', 'account', 'cid'));
    }
}
